package gui.menu.view;

import data.DataVector;
import data.classDiagram.ClassEntity;
import data.classDiagram.Entity;
import org.pushingpixels.flamingo.api.common.JCommandToggleButton;
import org.pushingpixels.flamingo.api.common.StringValuePair;
import org.pushingpixels.flamingo.api.common.icon.EmptyResizableIcon;
import org.pushingpixels.flamingo.api.common.icon.ResizableIcon;
import org.pushingpixels.flamingo.api.ribbon.JRibbonBand;
import org.pushingpixels.flamingo.api.ribbon.RibbonElementPriority;
import org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies;
import org.pushingpixels.flamingo.api.ribbon.resize.RibbonBandResizePolicy;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

/**
 * User: Szymon Kudzia
 * Date: 20.05.13
 * Time: 17:17
 */
public class ViewBand extends JRibbonBand implements ActionListener, Observer {
	List<JCommandToggleButton> galleryButtonsList;

	public ViewBand() {
		super("View", new EmptyResizableIcon(10));

		DataVector.getInstance().addObserver(this);

		this.setResizePolicies(Arrays.<RibbonBandResizePolicy>asList(
				new CoreRibbonResizePolicies.None(this.getControlPanel())));

		setUp();
		update();
	}

	private void setUp() {
		JCommandToggleButton button = new JCommandToggleButton("ClassDiagram",
				new EmptyResizableIcon(1));

		button.addActionListener(this);


		Map<RibbonElementPriority, Integer> visibleButtonCounts = new
				HashMap<RibbonElementPriority, Integer>();
		visibleButtonCounts.put(RibbonElementPriority.LOW, 1);
		visibleButtonCounts.put(RibbonElementPriority.MEDIUM, 1);
		visibleButtonCounts.put(RibbonElementPriority.TOP, 1);

		List<StringValuePair<List<JCommandToggleButton>>> galleryButtons =
				new ArrayList<StringValuePair<List<JCommandToggleButton>>>();

		galleryButtonsList = new ArrayList<JCommandToggleButton>();
		galleryButtonsList.add(button);

		galleryButtons
				.add(new StringValuePair<List<JCommandToggleButton>>(
						"Class diagram:", galleryButtonsList));
		galleryButtons
				.add(new StringValuePair<List<JCommandToggleButton>>(
						"State diagrams:", new ArrayList<JCommandToggleButton>()
				));

		this.addRibbonGallery("view", galleryButtons,
				visibleButtonCounts, 6, 4, RibbonElementPriority.TOP);

		selectButton(button);
	}

	private void update() {

		for (JCommandToggleButton b : galleryButtonsList)
			this.removeRibbonGalleryButtons("view", b);

		galleryButtonsList.clear();

		ResizableIcon icon = new EmptyResizableIcon(1);

		JCommandToggleButton button = new JCommandToggleButton("ClassDiagram", icon);
		button.addActionListener(this);
		galleryButtonsList.add(button);
		this.addRibbonGalleryButtons("view", "Class diagram:", button);

		if (null == DataVector.getInstance().getActiveStateDiagram()) {
			selectButton(button);
		}

		for (Entity entity : DataVector.getInstance().getEntityList()) {
			if (entity instanceof ClassEntity) {
				button = new JCommandToggleButton(entity.getName(), icon);
				button.addActionListener(this);
				addButton(button);

				if (null != DataVector.getInstance().getActiveStateDiagram()) {
					Entity active = DataVector.getInstance().getActiveStateDiagram();
					if(active.getName().equals(entity.getName()))
					{
						selectButton(button);
					}
				}
			}
		}
	}

	private void selectButton(JCommandToggleButton button) {
		this.setSelectedRibbonGalleryButton("view", button);
	}

	private void addButton(JCommandToggleButton button) {
		galleryButtonsList.add(button);
		this.addRibbonGalleryButtons("view", "State diagrams:", button);
	}

	public void actionPerformed(ActionEvent e) {
		JCommandToggleButton b = ((JCommandToggleButton)e.getSource());
		selectButton(b);
		DataVector.getInstance().setActiveStateDiagram(b.getText());
	}

	public void update(Observable o, Object arg) {
		update();
	}
}
